package com.weiaibenpao.demo.chislim.ui.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.ChangePhonePresenter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.utils.Utils;
import com.weiaibenpao.demo.chislim.view.ChangePhoneView;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangePhoneActivity extends BaseActivity<ChangePhoneView,ChangePhonePresenter> implements ChangePhoneView {

    @BindView(R.id.userNewPhone)
    EditText userNewPhone;
    @BindView(R.id.getVerifyNum)
    TextView getVerifyNum;
    @BindView(R.id.verify)
    EditText verify;


    private TimeCount time;
    private int userId;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_change_phone;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        userId = UserPreferences.getInstance().getInt(UserPreferences.USER_ID);
    }

    @OnClick({R.id.getVerifyNum,R.id.right_text,R.id.left_image})
    public void onClick(View view) {
      switch (view.getId()){
          case R.id.left_image:
              finish();
              break;
          case R.id.right_text:
              commitInfo();
              break;
          case R.id.getVerifyNum:
              sendCode();
              break;
      }
    }

    private void commitInfo() {
        if (!TextUtils.isEmpty(userNewPhone.getText().toString())&&!TextUtils.isEmpty(verify.getText().toString())){
            mPresenter.changePhone(userId,userNewPhone.getText().toString().trim(),verify.getText().toString().trim());
        }else {
            Toast.makeText(this, "请输入有效的信息", Toast.LENGTH_SHORT).show();

        }
    }

    private void sendCode() {
        //判断手机号码是否为空
      String  newPhone = userNewPhone.getText().toString().trim();
        if (!TextUtils.isEmpty(newPhone)) {
            //验证手机号码的正确性
            if(Utils.isMobileNO(newPhone)){
                //TODO 发送验证码
                Logger.e("userNewPhone:"+newPhone);
                mPresenter.getSeedCode(newPhone);
                //60秒倒计时
                time = new TimeCount(60000, 1000);
                time.start();
            } else {/**/
                Toast.makeText(this, this.getResources().getString(R.string.input_rightphone_text), Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, this.getResources().getString(R.string.input_rightphone_text), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public ChangePhonePresenter initPresenter() {
        return new ChangePhonePresenter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(time != null){
            time.cancel();
        }
    }


    @Override
    public void upDataPhone(UserBean userBean) {
        UserPreferences.getInstance().putString(UserPreferences.USER_PHONE,userBean.getUserPhone());
        Intent intent = new Intent();
        intent.putExtra(Constant.CLASS_BEAN,userBean.getUserPhone());
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    public void seedSuccess(String str) {

    }


    /**
     * 60秒倒计时
     */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getVerifyNum.setClickable(false);
            getVerifyNum.setText(getApplication().getResources().getString(R.string.show_verify_text) + millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            getVerifyNum.setText(getApplication().getResources().getString(R.string.get_repeatverify_text));
            getVerifyNum.setClickable(true);
        }
    }
}
